package com.hundsun.s3.mrs.core.task;

import org.apache.commons.lang.StringUtils;

import com.hundsun.s3.mrs.core.chain.ChainManager;
import com.hundsun.s3.mrs.core.chain.IChain;
import com.hundsun.s3.mrs.core.message.Message;
import com.hundsun.s3.mrs.core.repeater.CoreRepeater;
import com.hundsun.s3.mrs.core.talker.TalkerManager;
import com.hundsun.s3.mrs.core.type.TaskType;

/**
 * 默认任务
 * @author chenji23607
 *
 */
public class DefTask extends Task {
	
	private Message message;
	
	@SuppressWarnings("unused")
	private DefTask() {}

	public DefTask(Message message) {
		this.creater = TalkerManager.getTalker(message.getSenderId());
		this.message = message;
	}	

	public Message getMessage() {
		return message;
	}

	public void setMessage(Message message) {
		this.message = message;
	}
	
	
	/**
	 * 执行任务，取线程池里面的任务处理线程执行任务
	 */
	public void exec() {
		IChain cNew = ChainManager.duplicate("taskChain");
		cNew.doHandle(this, null, cNew);
	}
	

	/**
	 * 将自己(任务)放入该存放的数据结构中，并唤醒相应的处理线程
	 */
	public void up() {
		String taskType = this.getMessage().getTaskType();
		if(StringUtils.isBlank(taskType)) {
			return;
		}
		synchronized (CoreRepeater.class) {
			switch (taskType) {
			case TaskType.TASK_SEND:
				TaskManager.getSendTask().add(this);
				break;
			case TaskType.TASK_SR:
				TaskManager.getSrTask().add(this);
				break;
			case TaskType.TASK_CLOSE:
				TaskManager.getCloseTask().add(this);
				break;
			case TaskType.TASK_REDIRECT:
				TaskManager.getRedirectTask().add(this);
				break;
			case TaskType.TASK_EVALUATE:
				TaskManager.getEvaluateTask().add(this);
				break;
			default:
				break;
			}
			// 唤醒转发器
			CoreRepeater.class.notifyAll();
		}
	}

	@Override
	public void run() {
		exec();
	}

}
